from District import district
from River import river
from Intake import intake
from Station import station
from TimeBean import timeBean

def instantiate(data): #递归对入参的数据进行river以及其中数据内容的实例化
    r = river(id=data['id'],name=data['name'],end=data['end'],root=data['root'])
    for dis in data['districts']:
        d=district(id=dis['id'],name=dis['name'],area=dis['area'] ,person=dis['person'],person_city=dis['person_city'],person_county=dis['person_county'],plant=dis['plant'],iav=dis['iav'],regression=dis['regression'],aw_sum=dis['aw_sum'],aw=dis['aw'],iw_sum=dis['iw_sum'],iw=dis['iw'],dw_sum=dis['dw_sum'],dw=dis['dw'],ew_sum=dis['ew_sum'],ew=dis['ew'],gw_capacity=dis['gw_capacity'],gw_max=dis['gw_max'],gw=dis['gw'],w_wds=dis['w_wds'],w_other=dis['w_other'])
        r.districts.append(d)
    for tb in data['time_beans']:
        t = timeBean(inflow_intvl=tb['inflow_intvl'],inflow=tb['inflow'],inflow_upper=tb['inflow_upper'],outflow=tb['outflow'],flow_wdrl=tb['flow_wdrl'],sub_wdrl=tb['sub_wdrl'],level_begin=tb['level_begin'],level_end=tb['level_end'],v_begin=tb['v_begin'],v_end=tb['v_end'])
        r.time_beans.append(t)

    for element in data['elements']:
        if element['tip']=='river':
            e = instantiate(element)
            r.rivers.append(e)
            r.elements.append(e)

        if element['tip']=='station':
            e = station(element)
            for tb in element['time_beans']:
                t = timeBean(inflow_intvl=tb['inflow_intvl'], inflow=tb['inflow'], inflow_upper=tb['inflow_upper'],
                             outflow=tb['outflow'], flow_wdrl=tb['flow_wdrl'], sub_wdrl=tb['sub_wdrl'],
                             level_begin=tb['level_begin'], level_end=tb['level_end'], v_begin=tb['v_begin'],
                             v_end=tb['v_end'])
                e.time_beans.append(t)
            d = district(id=element['district']['id'], name=element['district']['name'], area=element['district']['area'], person=element['district']['person'],
                             person_city=element['district']['person_city'], person_county=element['district']['person_county'], plant=element['district']['plant'],
                             iav=element['district']['iav'], regression=element['district']['regression'], aw_sum=element['district']['aw_sum'], aw=element['district']['aw'],
                             iw_sum=element['district']['iw_sum'], iw=element['district']['iw'], dw_sum=element['district']['dw_sum'], dw=element['district']['dw'],
                             ew_sum=element['district']['ew_sum'], ew=element['district']['ew'], gw_capacity=element['district']['gw_capacity'], gw_max=element['district']['gw_max'],
                             gw=element['district']['gw'], w_wds=element['district']['w_wds'], w_other=element['district']['w_other'])
            e.district = d
            for dis in element['districts']:
                d = district(id=dis['id'], name=dis['name'], area=dis['area'], person=dis['person'],
                             person_city=dis['person_city'], person_county=dis['person_county'], plant=dis['plant'],
                             iav=dis['iav'], regression=dis['regression'], aw_sum=dis['aw_sum'], aw=dis['aw'],
                             iw_sum=dis['iw_sum'], iw=dis['iw'], dw_sum=dis['dw_sum'], dw=dis['dw'],
                             ew_sum=dis['ew_sum'], ew=dis['ew'], gw_capacity=dis['gw_capacity'], gw_max=dis['gw_max'],
                             gw=dis['gw'], w_wds=dis['w_wds'], w_other=dis['w_other'])
                e.districts.append(d)
            r.stations.append(e)
            r.elements.append(e)


        if element['tip']=='intake':
            e = intake(element)
            for tb in element['time_beans']:
                t = timeBean(inflow_intvl=tb['inflow_intvl'], inflow=tb['inflow'], inflow_upper=tb['inflow_upper'],
                             outflow=tb['outflow'], flow_wdrl=tb['flow_wdrl'], sub_wdrl=tb['sub_wdrl'],
                             level_begin=tb['level_begin'], level_end=tb['level_end'], v_begin=tb['v_begin'],
                             v_end=tb['v_end'])
                e.time_beans.append(t)
            d = district(id=element['district']['id'], name=element['district']['name'], area=element['district']['area'], person=element['district']['person'],
                             person_city=element['district']['person_city'], person_county=element['district']['person_county'], plant=element['district']['plant'],
                             iav=element['district']['iav'], regression=element['district']['regression'], aw_sum=element['district']['aw_sum'], aw=element['district']['aw'],
                             iw_sum=element['district']['iw_sum'], iw=element['district']['iw'], dw_sum=element['district']['dw_sum'], dw=element['district']['dw'],
                             ew_sum=element['district']['ew_sum'], ew=element['district']['ew'], gw_capacity=element['district']['gw_capacity'], gw_max=element['district']['gw_max'],
                             gw=element['district']['gw'], w_wds=element['district']['w_wds'], w_other=element['district']['w_other'])
            e.district = d
            r.intakes.append(e)
            r.elements.append(e)

    return r
#实例数据，根据类走
data={
  "id": "000000",
  "name": "根节点",
  "time_beans": [],
  "end": False,
  "root": True,
  "rivers": [],
  "stations": [],
  "intakes": [],
  "elements": [
      {
      "tip": "station",
      "id": "000001",
      "name": "stations",
      "t": 1,
      "distance_to_root": 0.01,
      "outflow_max": 0.01,
      "outflow_min": 0.01,
      "level_min": 0.01,
      "delta_t": 0.01,
      "river": {},
      "district":   {
            "id": "district",
            "name": "name",
            "area": 0.01,
            "person": [],
            "person_city": [],
            "person_county": [],
            "plant": [],
            "iav": [],
            "regression": [],
            "aw_sum": 0.01,
            "aw": [],
            "iw_sum": 0.01,
            "iw": [],
            "dw_sum": 0.01,
            "dw": [],
            "ew_sum": 0.01,
            "ew": [],
            "gw_capacity": 0.01,
            "gw_max": 0.01,
            "gw": [],
            "w_wds": 0.01,
            "w_other": 0.01
          },
      "capacity": 0.01,
      "time_beans": [],
      "upper": [],
      "down": [],
      "upper_station": [],
      "first": False,
      "end": True,
      "level_max": 0.01,
      "level_flood": 0.01,
      "v_max": 0.01,
      "v_flood": 0.01,
      "v_min": 0.01,
      "storage_coefficient": 0.01,
      "districts": [
          {
              "id": "district",
              "name": "name",
              "area": 0.01,
              "person": [],
              "person_city": [],
              "person_county": [],
              "plant": [],
              "iav": [],
              "regression": [],
              "aw_sum": 0.01,
              "aw": [],
              "iw_sum": 0.01,
              "iw": [],
              "dw_sum": 0.01,
              "dw": [],
              "ew_sum": 0.01,
              "ew": [],
              "gw_capacity": 0.01,
              "gw_max": 0.01,
              "gw": [],
              "w_wds": 0.01,
              "w_other": 0.01
          }
      ]
    }],
  "districts": [
  {
    "id": "district",
    "name": "name",
    "area": 0.01,
    "person": [],
    "person_city": [],
    "person_county": [],
    "plant": [],
    "iav": [],
    "regression": [],
    "aw_sum": 0.01,
    "aw": [],
    "iw_sum": 0.01,
    "iw": [],
    "dw_sum": 0.01,
    "dw": [],
    "ew_sum": 0.01,
    "ew": [],
    "gw_capacity": 0.01,
    "gw_max": 0.01,
    "gw": [],
    "w_wds": 0.01,
    "w_other": 0.01
  }
  ]
}

r=instantiate(data)
